package com.shiyi.service.pdf.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.shiyi.service.pdf.PoiWordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class PoiWordServiceImpl implements PoiWordService {


    /**
     * 可以生成目录，但是位置和页码都不对，不知道高版本的poi是否支持
     * 需要进一步研究下高版本的，目前的版本3.7
     * 当前最新版本5
     */
    @Override
    public void write2Docx() {
        // 创建一个新的Word文档
        XWPFDocument doc = new XWPFDocument();
        CTSdtBlock block = doc.getDocument().getBody().addNewSdt();

        CustomTOC customTOC = new CustomTOC(block);
//        doc.createTOC();
        // 创建目录
        XWPFParagraph paragraph = doc.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText("目录");
        run.setBold(true);
        paragraph.setStyle("TOCHeading");

        // 添加目录的占位符
        paragraph = doc.createParagraph();
        run = paragraph.createRun();
        run.setText(" ");
        run.addCarriageReturn();
        run.setText(" ");
        paragraph.setStyle("TOC1");
        // 更新目录
//        doc.createTOC();

        for (int i = 0; i < 10; i++) {
            // 添加标题和正文内容
            customTOC.addRowOnlyTitle(1, "1级title" + i);
            for (int j = 0; j < 10; i++) {
                customTOC.addRow(2, "2级title" + j, j, "这是第" + j + "章的内容。sdsdsdsd");
            }

        }

        for (int i = 0; i < 100; i++) {
            // 添加标题和正文内容
            paragraph = doc.createParagraph();
            run = paragraph.createRun();
            run.setText("第" + i + "章 引言");
            run.setBold(true);
            paragraph.setStyle("Heading1");

            paragraph = doc.createParagraph();
            run = paragraph.createRun();
            run.setText("这是第" + i + "章的内容。sdsdsdsd");
        }


        // 重复以上步骤添加更多的标题和内容

        // 写入文件
//        "测试wordTest"+ DateUtil.format(new Date(), DatePattern.PURE_DATETIME_FORMAT)
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(DateUtil.format(new Date(), DatePattern.PURE_DATETIME_FORMAT) + "WordWithTOC.docx");
            doc.write(out);
            out.close();
            doc.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


    //生成word
//    public void gen、

    /**
     * 写多个文档
     * 能生成文档，
     * 但是目录部分不能手动更新，页码不能自动计算，需要完善
     * 参考地址：
     * https://www.jianshu.com/p/0a32d8bd6878
     */
    @Override
    public void writeWordBanch() {
        XWPFDocument doc = new XWPFDocument();
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(new File(DateUtil.format(new Date(), DatePattern.PURE_DATETIME_FORMAT) + "WordWithTOC.docx"));

            FileRedWordDynamic wordFile = new FileRedWordDynamic(doc);
            wordFile.writeFileHead();
            wordFile.createCustomTOC();
            List newsData = new ArrayList<>();
            wordFile.writeAllNews(newsData);
//            wordFile.addItem2TOC();

            doc.write(out);
            out.close();
            doc.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
